package com.ln.drools.config;

import com.ln.drools.utils.KieUtils;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.KieSession;
import org.kie.internal.utils.KieHelper;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;

/**
 * Created by . on 17/7/31.
 */

@Service
public class ReloadDroolsRules {

    public void reload() throws UnsupportedEncodingException {
        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.write("src/main/resources/rules/temp.drl", loadRules());
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        Results results = kieBuilder.getResults();
        if (results.hasMessages(Message.Level.ERROR)) {
            System.out.println(results.getMessages());
            throw new IllegalStateException("### errors ###");
        }
        KieUtils.setKieContainer(kieServices.newKieContainer(getKieServices().getRepository().getDefaultReleaseId()));
        System.out.println("新规则重载成功");
    }

    private String loadRules() {
        // 从数据库加载的规则
        return "package plausibcheck.adress\n\n rule \"Postcode 6 numbers\"\n\n    when\n  then\n        System.out.println(\"规则2中打印日志：校验通过!\");\n end";

    }

    private KieServices getKieServices() {
        return KieServices.Factory.get();
    }

    public void reloadKModuleXML(){
        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.writeKModuleXML(loadKModuleXML());

        kfs.write("src/main/resources/rules1/temp.drl", loadksessionRules1());
        kfs.write("src/main/resources/rules2/temp.drl", loadksessionRules2());
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        Results results = kieBuilder.getResults();
        if (results.hasMessages(Message.Level.ERROR)) {
            System.out.println(results.getMessages());
            throw new IllegalStateException("### errors ###");
        }
        KieUtils.setKieContainer(kieServices.newKieContainer(getKieServices().getRepository().getDefaultReleaseId()));
        System.out.println("reloadKModuleXML新规则重载成功");
    }

    public void addKModule(){

    }
    private String loadKModuleXML(){
        return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
                "    <kbase name=\"rules\" packages=\"rules1\">\n" +
                "        <ksession name=\"ksession-rules\"/>\n" +
                "    </kbase>\n" +
                "    <kbase name=\"rules2\" packages=\"rules2\">\n" +
                "        <ksession name=\"ksession-rules2\"/>\n" +
                "    </kbase>\n" +
                "    <kbase name=\"rules3\" packages=\"rules3\">\n" +
                "        <ksession name=\"stateless-rules\"/>\n" +
                "    </kbase>\n" +
                "</kmodule>";
    }


    private String loadksessionRules1(){
        return "package plausibcheck.adress\n" +
                "\n" +
                "import com.ln.drools.model.Address;\n" +
                "import com.ln.drools.model.fact.AddressCheckResult;\n" +
                "\n" +
                "\n" +
                "rule \"rules\"\n" +
                "\n" +
                "    when\n" +
                "\n" +
                "    then\n" +
                "\n" +
                "\t\tSystem.out.println(\"rules\");\n" +
                "end";
    }
    private String loadksessionRules2(){
        return "package plausibcheck.adress\n" +
                "\n" +
                "import com.ln.drools.model.Address;\n" +
                "import com.ln.drools.model.fact.AddressCheckResult;\n" +
                "\n" +
                "\n" +
                "rule \"rules2\"\n" +
                "\n" +
                "    when\n" +
                "\n" +
                "    then\n" +
                "\n" +
                "\t\tSystem.out.println(\"rules2\");\n" +
                "end";
    }

    /**
     * 校验 规则
     * @return
     */
    public KieSession checkRules(){
        KieHelper kh = new KieHelper();
        kh.addContent("package plausibcheck.adress\n" +
                "\n" +
                "import com.ln.drools.model.Address;\n" +
                "import com.ln.drools.model.fact.AddressCheckResult;\n" +
                "\n" +
                "\n" +
                "rule \"rules2\"\n" +
                "\n" +
                "    when\n" +
                "\n" +
                "    then\n" +
                "\n" +
                "\t\tSystem.out.println(\"rules2 ch\");\n" +
                "end", ResourceType.DRL);
//        Results res = kh.verify();

        return kh.build().newKieSession();
    }


}
